package com.code.util;

import com.code.util.StringUtils;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.JarURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

public class JarUtils {
    public JarUtils() {
    }

    public static boolean copyFile(File toCopy, File destFile) {
        try {
            return copyStream(new FileInputStream(toCopy), (OutputStream)(new FileOutputStream(destFile)));
        } catch (FileNotFoundException var3) {
            var3.printStackTrace();
            return false;
        }
    }

    private static boolean copyFilesRecusively(File toCopy, File destDir) {
        assert destDir.isDirectory();

        if(!toCopy.isDirectory()) {
            return copyFile(toCopy, new File(destDir, toCopy.getName()));
        } else {
            File newDestDir = new File(destDir, toCopy.getName());
            if(!newDestDir.exists() && !newDestDir.mkdir()) {
                return false;
            } else {
                File[] var6;
                int var5 = (var6 = toCopy.listFiles()).length;

                for(int var4 = 0; var4 < var5; ++var4) {
                    File child = var6[var4];
                    if(!copyFilesRecusively(child, newDestDir)) {
                        return false;
                    }
                }

                return true;
            }
        }
    }

    public static boolean copyJarResourcesRecursively(File destDir, JarURLConnection jarConnection) throws IOException {
        JarFile jarFile = jarConnection.getJarFile();
        Enumeration e = jarFile.entries();

        while(e.hasMoreElements()) {
            JarEntry entry = (JarEntry)e.nextElement();
            if(entry.getName().startsWith(jarConnection.getEntryName())) {
                String filename = StringUtils.removeStart(entry.getName(), jarConnection.getEntryName());
                File f = new File(destDir, filename);
                if(!entry.isDirectory()) {
                    InputStream entryInputStream = jarFile.getInputStream(entry);
                    if(!copyStream(entryInputStream, f)) {
                        return false;
                    }

                    entryInputStream.close();
                } else if(!ensureDirectoryExists(f)) {
                    throw new IOException("Could not create directory: " + f.getAbsolutePath());
                }
            }
        }

        return true;
    }

    public static boolean copyResourcesRecursively(URL originUrl, File destination) {
        try {
            URLConnection e = originUrl.openConnection();
            return e instanceof JarURLConnection?copyJarResourcesRecursively(destination, (JarURLConnection)e):copyFilesRecusively(new File(originUrl.getPath()), destination);
        } catch (IOException var3) {
            var3.printStackTrace();
            return false;
        }
    }

    public static boolean copyResourcesRecursivelyExcludeTop(URL originUrl, File destination) {
        try {
            URLConnection e = originUrl.openConnection();
            if(e instanceof JarURLConnection) {
                return copyJarResourcesRecursively(destination, (JarURLConnection)e);
            } else {
                File topFolder = new File(originUrl.getPath());
                if(topFolder.isDirectory()) {
                    File[] var7;
                    int var6 = (var7 = topFolder.listFiles()).length;

                    for(int var5 = 0; var5 < var6; ++var5) {
                        File tempFile = var7[var5];
                        copyFilesRecusively(tempFile, destination);
                    }
                }

                return true;
            }
        } catch (IOException var8) {
            var8.printStackTrace();
            return false;
        }
    }

    private static boolean copyStream(InputStream is, File f) {
        try {
            return copyStream(is, (OutputStream)(new FileOutputStream(f)));
        } catch (FileNotFoundException var3) {
            var3.printStackTrace();
            return false;
        }
    }

    private static boolean copyStream(InputStream is, OutputStream os) {
        try {
            byte[] e = new byte[1024];
            boolean len = false;

            int len1;
            while((len1 = is.read(e)) > 0) {
                os.write(e, 0, len1);
            }

            is.close();
            os.close();
            return true;
        } catch (IOException var4) {
            var4.printStackTrace();
            return false;
        }
    }

    private static boolean ensureDirectoryExists(File f) {
        return f.exists() || f.mkdir();
    }
}
